<script setup>
import { onMounted, ref } from 'vue'
import dial from './dial.vue'
// 假设后续会创建API文件
// import { getAllApi, deleteApi } from '@/api/stocktaking/taskdetails'

const formData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const size = ref('default')
const background = ref(true)
const disabled = ref(false)

// 搜索表单
const form = ref({
  category: '',
  targetId: '',
  stocktakingTaskId: ''
})

// 类型选项
const categoryOptions = [
  { value: 1, label: '仓库' },
  { value: 2, label: '库区' },
  { value: 3, label: '库位' }
]

// 弹窗引用
const diaRef = ref(null)

// 分页相关方法
const handleSizeChange = (val) => {
  pageSize.value = val
  getAll()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getAll()
}

// 获取数据
const getAll = async () => {
  // 模拟数据获取
  // let res = await getAllApi(
  //   form.value.category,
  //   form.value.targetId,
  //   form.value.stocktakingTaskId,
  //   currentPage.value,
  //   pageSize.value
  // )
  // total.value = res.total
  // formData.value = res.rows

  // 临时模拟数据
  formData.value = [
    {
      id: 1,
      category: 1,
      targetId: 1001,
      stocktakingTaskId: 1,
      createTime: '2023-05-01 10:00:00',
      createBy: 'admin'
    },
    {
      id: 2,
      category: 2,
      targetId: 2001,
      stocktakingTaskId: 1,
      createTime: '2023-05-01 10:00:00',
      createBy: 'admin'
    },
    {
      id: 3,
      category: 3,
      targetId: 3001,
      stocktakingTaskId: 2,
      createTime: '2023-05-02 14:30:00',
      createBy: 'manager'
    }
  ]
  total.value = 3
}

// 搜索
const search = () => {
  currentPage.value = 1
  getAll()
}

// 重置搜索
const resetSearch = () => {
  form.value = {
    category: '',
    targetId: '',
    stocktakingTaskId: ''
  }
  search()
}

// 添加
const handleAdd = () => {
  if (diaRef.value) {
    diaRef.value.resetForm()
    diaRef.value.dialogVisible = true
  }
}

// 编辑
const handleEdit = (row) => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    // 模拟数据回显
    diaRef.value.dialogFormData = { ...row }
  }
}

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除这条记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // await deleteApi(id)
    ElMessage.success('删除成功')
    getAll()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 查看详情
const handleViewDetails = (row) => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    diaRef.value.dialogFormData = { ...row }
    diaRef.value.isViewMode = true
  }
}

onMounted(() => {
  getAll()
})
</script>

<template>
  <dial ref="diaRef" @refresh="getAll" />

  <!-- 搜索表单 -->
  <el-form :model="form" :inline="true" class="search-form">
    <el-form-item label="类型">
      <el-select v-model="form.category" placeholder="请选择类型" clearable>
        <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="目标ID">
      <el-input v-model="form.targetId" placeholder="请输入目标ID" />
    </el-form-item>
    <el-form-item label="任务ID">
      <el-input v-model="form.stocktakingTaskId" placeholder="请输入任务ID" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </el-form-item>
  </el-form>

  <!-- 操作按钮 -->
  <div class="toolbar">
    <el-button type="primary" @click="handleAdd">新增任务详情</el-button>
  </div>

  <!-- 数据表格 -->
  <el-table :data="formData" style="width: 100%" border>
    <el-table-column prop="id" label="主键ID" width="100" />
    <el-table-column prop="category" label="类型" width="120">
      <template #default="{ row }">
        <el-tag :type="row.category === 1 ? 'primary' : row.category === 2 ? 'success' : 'warning'">
          {{ row.category === 1 ? '仓库' : row.category === 2 ? '库区' : '库位' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="targetId" label="目标ID" width="120" />
    <el-table-column prop="stocktakingTaskId" label="任务ID" width="120" />
    <el-table-column prop="createTime" label="创建时间" width="180" />
    <el-table-column prop="createBy" label="创建人" width="120" />
    <el-table-column fixed="right" label="操作" width="200">
      <template #default="{ row }">
        <el-button type="primary" size="small" @click="handleViewDetails(row)">查看</el-button>
        <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <div class="pagination-container">
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 50]"
      :size="size" :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper"
      :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<style scoped>
.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.toolbar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>